package com.eight.group.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eight.group.entity.PageResult;
import com.eight.group.entity.QueryPageBean;
import com.eight.group.entity.Result;
import com.eight.group.utils.DateUtils;
import com.eight.group.vo.AddOrderVO;
import com.eight.group.vo.OrderAndMemberVO;
import com.xqx.eight.group.service.OrderService;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.sql.Date;
import java.util.List;

/**
 * @author：xingquanxiang createTime：2019/11/18 14:16
 * description:
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Reference
    private OrderService orderService;

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage (@RequestBody QueryPageBean queryPageBean){
        try {
            return  orderService.findPageByCondition(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(0L, null);
        }
    }

    /**
     * 新增预约
     * @param orderVO
     * @param setmealIds
     * @return
     */
    @RequestMapping("/add")
    public Result addOrder(@RequestBody AddOrderVO orderVO, Integer[] setmealIds){
        try {
            return orderService.add(orderVO, setmealIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "添加预约失败！");
        }
    }

    /**
     * 取消预约
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            orderService.delete(id);
            return new Result(true, "取消预约成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "取消预约失败！");
        }
    }

    /**
     * 确认预约
     * @param id
     * @return
     */
    @RequestMapping("/confirm")
    public Result confirm(Integer id){
        try {
            orderService.confirm(id);
            return new Result(true, "确认预约成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "确认预约失败！");
        }
    }

    /**
     * TODO 未实现
     * 编辑预约信息
     * @param orderVO
     * @param setmealIds
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody AddOrderVO orderVO, Integer[] setmealIds){
        try {
            return orderService.edit(orderVO, setmealIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "编辑预约失败！");
        }
    }

    /**
     * 到处excel表格
     * 档案号	姓名	手机号码	预约类型	预约状态	套餐名称	预约日期
     * @param req
     * @param resp
     * @return
     * @throws Exception
     */
    @RequestMapping("/export.do")
    public Result export(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        List<OrderAndMemberVO> list = orderService.findAll();
        //获得Excel模板文件绝对路径
        String temlateRealPath = req.getSession().getServletContext().getRealPath("template") + File.separator + "orderList.xlsx";
//        System.out.println(temlateRealPath);
        //读取模板文件创建Excel表格对象
        XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(new File(temlateRealPath)));

        XSSFSheet sheet = workbook.getSheetAt(0);//第一个分页
        for (int i = 0; i < list.size(); i++) {
            OrderAndMemberVO vo = list.get(i);

            Date orderDate = vo.getOrderDate();
            String orderType = vo.getOrderType();
            String orderStatus = vo.getOrderStatus();
            String fileNo = vo.getFileNo();
            String name = vo.getName();
            String siteName = vo.getSiteName();
            String phoneNumber = vo.getPhoneNumber();
            String setmealName = vo.getSetmealName();

            // 档案号	姓名	手机号码	预约类型	预约状态	套餐名称	预约日期
            //创建行
            XSSFRow row = sheet.createRow(i+1);

            if (fileNo != null && fileNo.length() > 0) {
                row.createCell(0).setCellValue(fileNo);
            }else{
                row.createCell(0).setCellValue("");
            }
            if (name != null && name.length() > 0) {
                row.createCell(1).setCellValue(name);
            }else{
                row.createCell(1).setCellValue("");
            }
            if (phoneNumber != null && phoneNumber.length() > 0) {
                row.createCell(2).setCellValue(phoneNumber);
            }else{
                row.createCell(2).setCellValue("");
            }
            if (orderType != null && orderType.length() > 0) {
                row.createCell(3).setCellValue(orderType);
            }else{
                row.createCell(3).setCellValue("");
            }
            if (orderStatus != null && orderStatus.length() > 0) {
                row.createCell(4).setCellValue(orderStatus);
            }else{
                row.createCell(4).setCellValue("");
            }

            if (setmealName != null && setmealName.length() > 0) {
                row.createCell(5).setCellValue(setmealName);
            }else{
                row.createCell(5).setCellValue("");
            }
            if (orderDate != null) {
                row.createCell(6).setCellValue(DateUtils.parseDate2String(orderDate));
            }else{
                row.createCell(6).setCellValue("");
            }
            if (siteName != null && siteName.length() > 0) {
                row.createCell(7).setCellValue(siteName);
            }else{
                row.createCell(7).setCellValue("");
            }
        }

        //通过输出流进行文件下载
        ServletOutputStream out = resp.getOutputStream();
        resp.setContentType("application/vnd.ms-excel");    //代表Excel文件类型
        resp.setHeader("content-Disposition","attachment;filename=orderList.xlsx");
        workbook.write(out);
        out.flush();
        out.close();
        workbook.close();
        return null;
    }




}
